require 'openssl'
require 'digest'
require 'jsonify'
module YiBuCommitService
	URL = "http://jzgr.mohurd.gov.cn:81/open.api"
	def self.qy_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    qyyibus = QYYiBu.where("upload_status = 1")
	    qyyibus.each do |qy|
	    	project = Project.find(qy.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode qy.requestSerialCode
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	qy.upload_status = 2
		      	qy.save
		      return 0
		    else
		      	qy.upload_status = 3
		      	qy.save
		    end
	    end
  	end
  	def self.cj_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    cjyibus = CJYiBu.where("upload_status = 1")
	    cjyibus.each do |cj|
	    	project = Project.find(cj.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode cj.requestSerialCode
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	cj.upload_status = 2
		      	cj.save
		      return 0
		    else
		      	cj.upload_status = 3
		      	cj.save
		    end
	    end
  	end

  	def self.bz_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    bzyibus = BZYiBu.where("upload_status = 1")
	    bzyibus.each do |bz|
	    	project = Project.find(bz.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode bz.requestSerialCode
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["result"]["teamSysno"]
		    	bz.upload_status = 2
		      	bz.save
		      	project_team = ProjectTeam.find(bz.bz_id)
		      	result = JSON.parse JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["result"]
		      	project_team.team_code = result["teamSysNo"]
		      	project_team.save
		      return 0
		    else
		      	bz.upload_status = 3
		      	bz.save
		    end
	    end
  	end
  	def self.ry_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    ryyibus = RYYiBu.where("upload_status = 1")
	    ryyibus.each do |ry|
	    	project_user = ProjectUser.find(ry.ry_id)
	    	user = User.find(project_user.user_id)
	    	project = Project.find(ry.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode ry.requestSerialCode
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	ry.upload_status = 2
		      	ry.save
		      	project_user = ProjectUser.find(ry.ry_id)
		      	project_user.upload_status = 2
		      	project_user.save
		      return 0
		    else
		    	qr_log = QrLog.new
			    qr_log.project_id = ryyibu.project_id
			    qr_log.MSG = "您发起的#{user.name}员工信息上传失败，错误原因为："+ JSON.parse(respons.body.force_encoding('UTF-8'))["message"]
			    qr_log.save
		      	ry.upload_status = 3
		      	ry.save
		    end
	    end
  	end
  	def self.kq_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    kqyibus = KQYiBu.where("upload_status = 1")
	    kqyibus.each do |kq|
	    	project = Project.find(kq.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode kq.requestSerialCode
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+ URI.encode_www_form_component(data)
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	kq.upload_status = 2
		      	kq.save
		      	sign = Sign.find(kq.kq_id)
		      	sign.upload_status = 2
		      	sign.save
		      return 0
		    else
		      	kq.upload_status = 3
		      	kq.save
		    end
	    end
  	end
  	def self.gz_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    gzyibus = GZYiBu.where("upload_status = 1")
	    gzyibus.each do |gz|
	    	project = Project.find(gz.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode gz.requestSerialCode
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	gz.upload_status = 2
		      	gz.save
		      	parolldetail = PayrollDetail.find(gz.gz_id)
		      	parolldetail.upload_status = 2
		      	parolldetail.payrollCode = JSON.parse(JSON.parse(respons.body.force_encoding('UTF-8'))["data"]["result"])[0]["payrollCode"]
		      	parolldetail.save
		      return 0
		    else
		      	gz.upload_status = 3
		      	gz.save
		    end
	    end
  	end

  	def self.jtc_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    jtcyibus = JTCYiBu.where("upload_status = 1")
	    jtcyibus.each do |jtc|
	    	project = Project.find(jtc.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode jtc.requestSerialCode
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	jtc.upload_status = 2
		      	jtc.save
		      	if jtc.jtc_type = 1
		      		project_user = ProjectUser.find(jtc.jtc_id)
			      	project_user.jc_upload_status = 2
			      	project_user.save
		      	else
		      		project_user = ProjectUser.find(jtc.jtc_id)
			      	project_user.tc_upload_status = 2
			      	project_user.save
		      	end		      	
		      return 0
		    else
		      	jtc.upload_status = 3
		      	jtc.save
		    end
	    end
  	end
  	def self.ht_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    project_users = ProjectUser.where("contract_upload_status = 1")
	    project_users.each do |pu|
	    	project = Project.find(pu.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode pu.contract_yibu
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	pu.contract_upload_status = 2
		      	pu.save		      	
		      return 0
		    else
		      	pu.contract_upload_status = 3
		      	pu.save
		    end
	    end
  	end
  	def self.px_qr()
	    conn = Faraday.new(:url => URL)
	    method = "AsyncHandleResult.Query"
	    timestamp = WorkManageService.get_timestamp()
	    nonce = rand(999999999).to_s
	    project_users = ProjectUser.where("train_upload_status = 1")
	    project_users.each do |pu|
	    	project = Project.find(pu.project_id)
		    request_data = Jsonify::Builder.new
		    request_data.requestSerialCode pu.contract_yibu
		    data = request_data.compile!
		    sign = WorkManageService.get_sign(project.app_id, data, method, timestamp, nonce, project.secret_key)
		    req_body = "method="+method+"&format="+WorkManageService::Format+"&version="+WorkManageService::Version+"&appid="+project.app_id+"&timestamp="+timestamp+"&nonce="+nonce+"&sign="+sign+"&data="+data
		    respons = conn.post do |req|
		      req.body = req_body.to_s
		    end
		    if JSON.parse(respons.body.force_encoding('UTF-8'))["code"] == "0"
		    	pu.train_upload_status = 2
		      	pu.save		      	
		      return 0
		    else
		      	pu.train_upload_status = 3
		      	pu.save
		    end
	    end
  	end
end